package com.whpost.service.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whpost.service.dao.t.THyxxDao;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.tools.Aes;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.XSSFilterJSON;
import jakarta.annotation.Resource;

import java.util.Map;

public class H5BaseServices implements IBaseService {

    @Resource
    private THyxxDao tHyxxDao;

    /**
     * 加密返回 200和500只表示是否返回成功，具体业务逻辑，参照data里面的code;
     * @param resultInfo
     * @param loginId
     * @return
     */
    public ResultInfo backRtn(ResultInfo resultInfo,String loginId) {
        JSONObject jsonObject1;
        String data="";
        try{
            jsonObject1 =  (JSONObject) JSONObject.toJSON(resultInfo.getData());
            data = JSONObject.toJSONString(jsonObject1);
        }catch (Exception e){
            data = (String)resultInfo.getData();
        }
        try{
            ToolBean.info("backRtn获取的data:["+data+"]");
            String aeskey = tHyxxDao.getAesKey(loginId);
            String rtnStr = Aes.encrypt(data,aeskey);
            ToolBean.info("backRtn准备返回的rtnStr:["+rtnStr+"]");
            return new ResultInfo(resultInfo.getCode(), resultInfo.getMsg(),rtnStr);
        }catch (Exception e){
            ToolBean.info("BaseServices.backRtn返回失败！");
            return new ResultInfo(500, "返回失败！",null);
        }
    }

    public JSONObject getData(Map<String, Object> map,String loginId) {
        JSONObject jsonObjectMap = (JSONObject) JSON.toJSON(map);
        String data = "";
        if (jsonObjectMap.containsKey("data")) data = jsonObjectMap.getString("data");
        ToolBean.info("接收的data："+data);
        String aeskey = gettHyxxDao().getAesKey(loginId);
        String dec_data = Aes.decrypt(data,aeskey);
        ToolBean.info("接收的data解密后："+dec_data);
        dec_data = XSSFilterJSON.xssFilterJson(dec_data);
        ToolBean.info("接收的data解密后过滤后："+dec_data);
        JSONObject jsonObject = JSONObject.parseObject(dec_data);
        return jsonObject;
    }

    public JSONObject getDataStr(String data,String loginId) {
        ToolBean.info("接收的data："+data);
        String aeskey = gettHyxxDao().getAesKey(loginId);
        String dec_data = Aes.decrypt(data,aeskey);
        ToolBean.info("接收的data解密后："+dec_data);
        dec_data = XSSFilterJSON.xssFilterJson(dec_data);
        ToolBean.info("接收的data解密后过滤后："+dec_data);
        JSONObject jsonObject = JSONObject.parseObject(dec_data);
        return jsonObject;
    }

    public THyxxDao gettHyxxDao() {
        return tHyxxDao;
    }
}
